const { toUrl,globalData } = getApp()
import { request } from '@/utils/request'
Page({
    toUrl,
    data: {
        list: [],
        content: '',
        currentIndex: 0,
        currentName: '选择反馈类型',
        imgList: [],
        globalData,
        baseUri: globalData.baseUrl
    },

    blur ({ detail }) {
        this.setData({
            content: detail.value
        })
    },
    changePhoto(e){
        if(this.data.imgList.length >= 4) {
            return
        }
        let { url } = e.detail;
        const imgList = this.data.imgList
        imgList.push(url)
        this.setData({
            imgList: imgList
        })
        //修改成功
    },
    //获取用户反馈类型
    getFeedBackList() {
        const that = this
        request('user/feedbackcat').then(res => {
            that.setData({
                list: res.data
            })
        })
    },
    //选择反馈类型
    bindPickerChange(e) {
        const index = e.detail.value
        const obj = this.data.list[index]
        this.setData({
            currentIndex: index,
            currentName: obj.name
        })
    },
    close(e) {
        const index = e.currentTarget.dataset.index
        const imgList = this.data.imgList
        imgList.splice(index,1)
        this.setData({
            imgList: imgList
        })
        console.log(this.data.imgList)
    },
    //提交用户反馈user/feedback
    subFeedBack() {
        let imgStr = ''
        if(this.data.imgList.length == 1) {
            imgStr = this.data.imgList[0]
        }else {
            imgStr = this.data.imgList[0]
            const len = this.data.imgList.length
            for(let i = 1; i < len; ++i ) {
                if(this.data.imgList[i]) {
                    imgStr = imgStr + ',' + this.data.imgList[i]
                }
            }
        }
        const data = {
            cat_id: this.data.list[this.data.currentIndex].id,
            content: this.data.content,
            images: imgStr
        }
        request('user/feedback',{data}).then(res => {
            if(res.errcode == 1) {
                wx.showToast({
                  title: res.message,
                })
                setTimeout(() => {
                    wx.navigateBack({
                      delta: 1,
                    })
                },500)
            }
        })
    },
    onLoad(options) {
        this.getFeedBackList()
        this.setData({
            baseUri: globalData.baseUrl
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})